<?php
namespace app\yticnus\Controller;
use app\admin\Controller\Export;
class Activity extends Common {

	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();
	}

	/*
	*	添加大转盘界面
	*/
	public function wheel_add(){
		if(request()->isPost()){
			$name = input("name");
			$odds = input('odds');
			$sort = input('sort');
			$data['name'] = $name;
			$data['odds'] = $odds;
            $data['sort'] = $sort;
            $data['a_name'] = '大转盘';
			$id=input('id');
			if($id){
				db('activity_prize')->where("id='$id'")->where('a_name','大转盘')->update($data);
			}else {
				$da=db('activity_prize')->where("name='$name'")->find();
				if($da) $this->error('奖品已存在');
				db('activity_prize')->insert($data);
			}
			$this->success("操作成功!",url('activity/wheel_index'));
		}
		return 	$this->fetch();
	}

	/*
	*	编辑大转盘界面
	*/
	public function wheel_edit(){
        $id  = input("id");
        $where['id'] = $id;
        $where['a_name'] = '大转盘';
		$res = db('activity_prize')->where($where)->find();
		$this->assign("res",$res);
		return 	$this->fetch();
	}
	/*
	*	大转盘列表
	*/
	public function wheel_index(){
        $data = db('activity_prize')->where('a_name','大转盘')->order('sort desc')->paginate(10);
        $this->assign('data',$data);
		return $this->fetch();
	}

	/*
	*	删除大转盘
	*/
	public function wheel_del(){
		$id = input("id");
		if(!$id) $this->error('参数有误');
		$da=db('activity_prize')->where("id='$id'")->find();
		if(!$da) $this->error('奖品不存在');
		$res=db('activity_prize')->where("id='$id'")->delete();
		if(!$res) $this->error('删除失败');
		$this->success("删除成功!",url('activity/wheel_index'));
	}


	/*
	*	添加砸金蛋界面
	*/
	public function egg_add(){
		if(request()->isPost()){
			$name = input("name");
			$odds = input('odds');
			$sort = input('sort');
			$data['name'] = $name;
			$data['odds'] = $odds;
            $data['sort'] = $sort;
            $data['a_name'] = '砸金蛋';
			$id=input('id');
			if($id){
				db('activity_prize')->where("id='$id'")->update($data);
			}else {
				$da=db('activity_prize')->where(["name"=>$name,"a_name"=>'砸金蛋'])->find();
				if($da) $this->error('奖品已存在');
				db('activity_prize')->insert($data);
			}
			$this->success("操作成功!",url('activity/egg_index'));
		}
		return 	$this->fetch();
	}

	/*
	*	编辑砸金蛋界面
	*/
	public function egg_edit(){
        $id  = input("id");
        $where['id'] = $id;
        $where['a_name'] = '砸金蛋';
		$res = db('activity_prize')->where($where)->find();
		$this->assign("res",$res);
		return 	$this->fetch();
	}
	/*
	*	砸金蛋列表
	*/
	public function egg_index(){
        $data = db('activity_prize')->where('a_name','砸金蛋')->order('sort desc')->paginate(10);
        $this->assign('data',$data);
		return $this->fetch();
	}

	/*
	*	删除砸金蛋
	*/
	public function egg_del(){
		$id = input("id");
		if(!$id) $this->error('参数有误');
		$da=db('activity_prize')->where("id='$id'")->find();
		if(!$da) $this->error('奖品不存在');
		$res=db('activity_prize')->where("id='$id'")->delete();
		if(!$res) $this->error('删除失败');
		$this->success("删除成功!",url('activity/egg_index'));
	}


	/*
	*	添加摇一摇界面
	*/
	public function shake_add(){
		if(request()->isPost()){
			$name = input("name");
			$odds = input('odds');
			$sort = input('sort');
			$data['name'] = $name;
			$data['odds'] = $odds;
            $data['sort'] = $sort;
            $data['a_name'] = '摇一摇';
			$id=input('id');
			if($id){
				db('activity_prize')->where("id='$id'")->update($data);
			}else {
				$da=db('activity_prize')->where(["name"=>$name,"a_name"=>'摇一摇'])->find();
				if($da) $this->error('奖品已存在');
				db('activity_prize')->insert($data);
			}
			$this->success("操作成功!",url('activity/shake_index'));
		}
		return 	$this->fetch();
	}

	/*
	*	编辑摇一摇界面
	*/
	public function shake_edit(){
        $id  = input("id");
        $where['id'] = $id;
        $where['a_name'] = '摇一摇';
		$res = db('activity_prize')->where($where)->find();
		$this->assign("res",$res);
		return 	$this->fetch();
	}
	/*
	*	摇一摇列表
	*/
	public function shake_index(){
        $data = db('activity_prize')->where('a_name','摇一摇')->order('sort desc')->paginate(10);
        $this->assign('data',$data);
		return $this->fetch();
	}

	/*
	*	删除摇一摇
	*/
	public function shake_del(){
		$id = input("id");
		if(!$id) $this->error('参数有误');
		$da=db('activity_prize')->where("id='$id'")->find();
		if(!$da) $this->error('奖品不存在');
		$res=db('activity_prize')->where("id='$id'")->delete();
		if(!$res) $this->error('删除失败');
		$this->success("删除成功!",url('activity/shake_index'));
	}


	/*
	*	活动中奖记录
	*/
	public function record(){
		if(input('export')){
			$this->export();exit;
		}
		$keyword = input('keyword');
		$where = [];
		if($keyword != ''){
			$where['m.nickname'] = array('like','%'.$keyword.'%');
		}
		$data = db('activity_record')->alias('ar')->join('member m','m.id = ar.m_id')->join('activity_prize ap','ap.id = ar.p_id')->field('ar.id,m.nickname,ar.time,ap.name,ap.a_name,ar.status')->where($where)->order('ar.time desc')->paginate(10);
		$this->assign('data',$data);
		$this->assign('keyword',$keyword);
		return $this->fetch();
	}

	/*
	*	中奖成功
	*/
	public function recordy(){
		$id = input("id");
		if(!$id) $this->error('参数有误');
		$da=db('activity_record')->where("id='$id'")->find();
		if(!$da) $this->error('记录不存在');
		if($da['status'] == '0'|| $da['status'] == '-1'){
			$data['status'] = '1';
			$res = db('activity_record')->where('id',$id)->update($data);
		}elseif($da['status'] == '1'){
			$this->error('请勿重复操作');
		}
		if(!$res) $this->error('操作失败');
		$this->success("操作成功!",url('activity/record'));
	}

	/*
	*	中奖失败
	*/
	public function recordn(){
		$id = input("id");
		if(!$id) $this->error('参数有误');
		$da=db('activity_record')->where("id='$id'")->find();
		if(!$da) $this->error('记录不存在');
		if($da['status'] == '0'|| $da['status'] == '1'){
			$data['status'] = '-1';
			$res = db('activity_record')->where('id',$id)->update($data);
		}elseif($da['status'] == '-1'){
			$this->error('请勿重复操作');
		}
		if(!$res) $this->error('操作失败');
		$this->success("操作成功!",url('activity/activity'));
	}

	/*
	*	导出excel记录
	*/
	public function export()
	{
		ini_set('memory_limit','1024M');
		set_time_limit ( 0 );

		$headArr = array("会员名称","中奖时间","奖品名称","活动名称");
		$list = db('activity_record')->select();
		$member=db("member");
		$ap = db("activity_prize");
		$data=array();
		foreach($list as $k => $v){
			$re = $member->where('id',$v['m_id'])->field('nickname')->find();
			$da = $ap->where('id',$v['p_id'])->field('name,a_name')->find();
			$data[$k][] = $re['nickname'];
			$data[$k][] = date("Y-m-d H:i",$v['time']);
			$data[$k][] = $da['name'];
			$data[$k][] = $da['a_name'];
		}
		$filename='中奖记录-'.date("Y-m-d",time());
		(new Export())->ImportExcel($filename,$headArr,$data);
	}
}